<template>
  <div :class="className" :style="{height: height, width: width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      // console.log(this.chartData)
      let use = this.chartData.use
      use = use.toFixed(2)
      let total = this.chartData.total
      total = total.toFixed(2)

      let loadRate
      let free
      if (Number(use) > Number(total)) {
        // console.log("分支1")
        loadRate = 100
        free = 0
      } else {
        // console.log("分支2")
        loadRate = (use / total * 100).toFixed(2)
        // console.log(use / total * 100)
        // console.log((use / total * 100).toFixed(2))
        free = total - use
      }

      // console.log(use)
      // console.log(total)
      // console.log(loadRate)
      // console.log(free)


      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          // formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        // legend: {
        //   left: 'center',
        //   bottom: '10',
        //   data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        // },
        // series: [
        //   {
        //     name: 'WEEKLY WRITE ARTICLES',
        //     type: 'pie',
        //     roseType: 'radius',
        //     radius: [15, 95],
        //     center: ['50%', '38%'],
        //     data: [
        //       { value: 320, name: 'Industries' },
        //       { value: 240, name: 'Technology' },
        //       // { value: 149, name: 'Forex' },
        //       // { value: 100, name: 'Gold' },
        //       // { value: 59, name: 'Forecasts' }
        //     ],
        //     animationEasing: 'cubicInOut',
        //     animationDuration: 2600
        //   }
        // ]
        series: [
          {
            name: '负荷率监测',
            type: 'pie',
            center: ['50%', '50%'],
            radius: ['70%', '100%'],
            // radius: '100%',
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center',
              normal: {
                show: true,
                position: 'center',
                color: '#4c4a4a',
                formatter: '{total|' + loadRate + '}%' + '\n' + '{active|负荷率}',
                rich: {
                  total: {
                    fontSize: 12,
                    fontFamily: "微软雅黑",
                    color: '#454c5c'
                  },
                  active: {
                    fontFamily: "微软雅黑",
                    fontSize: 12,
                    color: '#6c7a89',
                    lineHeight: 30,
                  },
                }
              },
            },
            emphasis: {
              show: true,

            },
            labelLine: {
              show: false
            },
            data: [
              {value: use, name: '负载', itemStyle: {color: '#04DBA2'}},
              {value: free, name: '余量', itemStyle: {color: '#D2D2D2'}}
            ]
          }
        ]
      })
    }
  }
}
</script>
